package zhuyun.code

import java.awt.BorderLayout
import java.awt.Font
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import javax.swing.*

fun main(args:Array<String>){
    PictureFrame()
}
class PictureFrame():JFrame(),ActionListener{
    private lateinit var ag:ViewSpotSet
    private lateinit var  it:ViewSpotIterator
    private var obj:WyViewSpot? = null
    init {
        title = "中国最美乡村“婺源”的部分风景图"
        this.isResizable = false
        ag = WyViewSpotSet()
        ag.add(WyViewSpot("江湾", "江湾景区是婺源的一个国家5A级旅游景区，景区内有萧江宗祠、永思街、滕家老屋、婺源人家、乡贤园、百工坊等一大批古建筑，精美绝伦，做工精细。"))
        ag.add(WyViewSpot("李坑", "李坑村是一个以李姓聚居为主的古村落，是国家4A级旅游景区，其建筑风格独特，是著名的徽派建筑，给人一种安静、祥和的感觉。"))
        ag.add(WyViewSpot("思溪延村", "思溪延村位于婺源县思口镇境内，始建于南宋庆元五年（1199年），当时建村者俞氏以（鱼）思清溪水而名。"))
        ag.add(WyViewSpot("晓起村", "晓起有“中国茶文化第一村”与“国家级生态示范村”之美誉，村屋多为清代建筑，风格各具特色，村中小巷均铺青石，曲曲折折，回环如棋局。"))
        ag.add(WyViewSpot("菊径村", "菊径村形状为山环水绕型，小河成大半圆型，绕村庄将近一周，四周为高山环绕，符合中国的八卦“后山前水”设计，当地人称“脸盆村”。"))
        ag.add(WyViewSpot("篁岭", "篁岭是著名的“晒秋”文化起源地，也是一座距今近六百历史的徽州古村；篁岭属典型山居村落，民居围绕水口呈扇形梯状错落排布。"))
        ag.add(WyViewSpot("彩虹桥", "彩虹桥是婺源颇有特色的带顶的桥——廊桥，其不仅造型优美，而且它可在雨天里供行人歇脚，其名取自唐诗“两水夹明镜，双桥落彩虹”。"))
        ag.add(WyViewSpot("卧龙谷", "卧龙谷是国家4A级旅游区，这里飞泉瀑流泄银吐玉、彩池幽潭碧绿清新、山峰岩石挺拔奇巧，活脱脱一幅天然泼墨山水画。"))
        it = ag.getIterator()
        obj = it.first()
        obj?.let { this.showPicture(it.name,it.introduce) }
    }

    private fun showPicture(name:String, introduce:String){
        val cp = this.contentPane
        val picturePanel = JPanel()
        val controlPanel = JPanel()
        val fileName = "src/images/$name.jpg"
        val lb = JLabel(name,ImageIcon(fileName),JLabel.CENTER)
        val ta = JTextArea(introduce)
        lb.horizontalTextPosition = JLabel.CENTER
        lb.verticalTextPosition = JLabel.TOP
        lb.font = Font("宋体", Font.BOLD,20)
        ta.lineWrap = true
        ta.isEditable = false
        picturePanel.layout = BorderLayout(5,5)
        picturePanel.add("Center",lb)
        picturePanel.add("South",ta)
        val first = JButton("第一张")
        val next = JButton("下一张")
        val previous = JButton("上一张")
        val last = JButton("最末张")
        first.addActionListener(this)
        next.addActionListener(this)
        previous.addActionListener(this)
        last.addActionListener(this)
        controlPanel.add(first)
        controlPanel.add(next)
        controlPanel.add(previous)
        controlPanel.add(last)
        cp.add("Center",picturePanel)
        cp.add("South",controlPanel)
        this.setSize(630,550)
        this.isVisible = true
        this.defaultCloseOperation = JFrame.EXIT_ON_CLOSE
    }
    override fun actionPerformed(e: ActionEvent?) {
        val command = e?.actionCommand
        when(command){
            "第一张"->{
                obj = it.first()
                obj?.let { this.showPicture(it.name,it.introduce) }
            }
            "下一张"->{
                obj = it.next()
                obj?.let { this.showPicture(it.name,it.introduce) }


            }
            "上一张"->{
                obj = it.previous()
                obj?.let { this.showPicture(it.name,it.introduce) }
            }
            "最末张"->{
                obj = it.last()
                obj?.let { this.showPicture(it.name,it.introduce) }
            }
        }

    }

}
class WyViewSpot(val name:String,val introduce:String)
interface ViewSpotSet{
    fun add(obj:WyViewSpot)
    fun remove(obj:WyViewSpot)
    fun getIterator():ViewSpotIterator
}
interface ViewSpotIterator{
    fun hasNext():Boolean
    fun first():WyViewSpot
    fun next():WyViewSpot?
    fun previous():WyViewSpot?
    fun last():WyViewSpot
}
class WyViewSpotSet:ViewSpotSet{
    private val list = mutableListOf<WyViewSpot>()
    override fun remove(obj: WyViewSpot) {
        list.remove(obj)
    }

    override fun getIterator(): ViewSpotIterator {
        return WyViewSpotIterator(list)
    }

    override fun add(obj: WyViewSpot) {
        list.add(obj)
    }

}
class WyViewSpotIterator(private val list:List<WyViewSpot>):ViewSpotIterator{
    private var index = 0
    override fun hasNext(): Boolean {
        return index+1<list.size
    }

    override fun first(): WyViewSpot {
        index = 0
        return list[index]
    }

    override fun next(): WyViewSpot? {
        if(hasNext()){
            return list[++index]
        }
        return null
    }

    override fun previous(): WyViewSpot? {
        if(index - 1>=0){
            return list[--index]
        }
        return null
    }

    override fun last(): WyViewSpot {
        index = list.size-1
        return list[index]
    }

}